package com.adwhirl.eventadapter;

import net.youmi.android.AdManager;
import net.youmi.android.AdView;
import net.youmi.android.AdViewListener;
import android.app.Activity;
import android.view.View;
import android.widget.RelativeLayout;
import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlLayout.ViewAdRunnable;
import com.adwhirl.eventadapter.IGmAdWhirlEventAdapter.GmAdWhirlCustomEventAdapter;
import com.adwhirl.obj.Ration;

class GmAdWhirlEventAdapter_cn_youmi extends GmAdWhirlCustomEventAdapter implements AdViewListener {
	
	private AdView	mADView;
	
	public GmAdWhirlEventAdapter_cn_youmi(AdWhirlLayout adLayout, Ration obj) {
		super(adLayout, obj);
	}
	
	@Override
	protected void init(Ration obj) {
		gmEventAdapterLog("youmi->init");
		AdWhirlLayout adLayout = getAdwhirlLayout();
		Activity activity = getAdwhirlActivity();
		if (adLayout != null && activity != null) {
			AdManager.init(activity, obj.key, obj.key2, 300000, false);
			mADView = new AdView(activity);
			mADView.setAdViewListener(this);
			
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			adLayout.addView(mADView, lp);
			
			// Show AD for certain seconds (extra.cycleTime).
			rotateThreadedDelayed();
			gmEventAdapterLog("youmi->rotateThreadedDelayed");
		}
	}
	
	@Override
	public void dispose() {
		gmEventAdapterLog("youmi->dispose");
		AdWhirlLayout tmpLayout = getAdwhirlLayout();
		if (tmpLayout != null) {
			if (mADView != null) {
				mADView.setVisibility(View.GONE);
				mADView.setAdViewListener(null);
				mADView = null;
				gmEventAdapterLog("youmi->removed");
			}
		}
		super.dispose();
	}
	
	@Override
	public void onConnectFailed(AdView arg0) {
		gmEventAdapterLog("youmi->onConnectFailed");
		if (isActiveAdView(mADView)) {
			if (!isAdFetchDone()) {
				doRollover();
				setAdFetchDone(true);
				gmEventAdapterLog("youmi->doRollover");
			}
		}
	}
	
	@Override
	public void onAdViewSwitchedAd(AdView arg0) {
		gmEventAdapterLog("youmi->onAdViewSwitchedAd");
		AdWhirlLayout adWhirlLayout = getAdwhirlLayout();
		AdView adView = (AdView) arg0;
		adWhirlLayout.adWhirlManager.resetRollover();
		adWhirlLayout.removeAllViews();
		adWhirlLayout.handler.post(new ViewAdRunnable(adWhirlLayout, adView));
		adWhirlLayout.rotateThreadedDelayed();
	}
	
}
